#!/usr/bin/env python
# -*- coding:utf-8 -*-
import SocketServer
import os
class MyServer(SocketServer.BaseRequestHandler):

    def handle(self):       #函数名必须是handle，源码BaseRequestHandler预定义；
        print "--got connection from ",self.client_address
        while True:
            data=self.request.recv(1024)
            print "Recv commmands :",data
            if data=="exit":
                break
            cmd_res=os.popen(data).read()
            self.request.send(str(len(cmd_res)))
            print self.request.recv(1024)
            self.request.sendall(cmd_res)

if __name__ == '__main__':
    server = SocketServer.ThreadingTCPServer(('127.0.0.1',8009),MyServer)
    server.serve_forever()